const http=require('http')
const fs=require('fs')
const path=require('path')
var argv=process.argv;

http.createServer((req,res)=>{
    res.writeHead(200,{"Content-Type":'text/html;charset=utf-8'})

    var getin=argv[2]
    var filePath=path.join(__dirname,"/"+getin)
    var filePath2=path.join(__dirname,"/fileReader1.js")

    if(getin==null){
        var fd=fs.openSync(filePath2,'r')
        var buf=Buffer.alloc(16384);
        fs.readSync(fd,buf)
        res.end(buf.toString('utf-8'))
        fs.close(fd,(err)=>[
            console.log(err)
        ])
    }else{
        fs.access(filePath,fs.constants.F_OK,(err)=>{//判断当前目录中是否存在
            if(!err){
                var fd=fs.openSync(filePath,'r')

                var buf=Buffer.alloc(16384);
                fs.readSync(fd,buf)
                
                res.end(buf.toString('utf-8'))
                fs.close(fd,(err)=>[
                    console.log(err)
                ])
                
            }else{
                console.log('该文件不在该目录下')
            }
        })
    }
    
}).listen(8081)




